package com.tys.algorithm.bak.code1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeMap;

public class Code28_Comparator {
    public static class Student {
        public String name;
        public int id;
        public int age;

        public Student(String name, int id, int age) {
            this.name = name;
            this.id = id;
            this.age = age;
        }
    }

    /**
     * 任何比较器：
     * compare方法的统一规范：
     * <0, 第一个排前面
     * >0, 第二个排前面
     * =0，谁在前都可以
     */
    public static class AgeShengOrder implements Comparator<Student> {

        @Override
        public int compare(Student o1, Student o2) {

            //按照年龄从小到大
//            if(o1.age < o2.age){
//                return -1;
//            }else if(o1.age > o2.age){
//                return 1;
//            }else{
//                return 0;
//            }

            return o1.age - o2.age;
        }
    }

    //根据id从小到大，id一样时，按照年龄从大到小
    public static class IdShengAgeJiangOrder implements Comparator<Student> {

        @Override
        public int compare(Student o1, Student o2) {
            return o1.id != o2.id ? (o1.id - o2.id) : (o2.age - o1.age);
        }
    }

    //根据id升序：从小到大
    public static class IdAscendingComparator implements Comparator<Student> {

        @Override
        public int compare(Student o1, Student o2) {
            return o1.id - o2.id;
        }
    }

    //根据id降序：从大到小
    public static class IdDescendingComparator implements Comparator<Student> {

        @Override
        public int compare(Student o1, Student o2) {
            return o2.id - o1.id;
        }
    }

    //id排序：从小到大，id一样，age排序：从大到小
    public static class IdInAgeDe implements Comparator<Student> {

        @Override
        public int compare(Student o1, Student o2) {
            return o1.id != o2.id ? o1.id - o2.id : (o2.age - o1.age);
        }
    }

    public static void printStudents(Student[] students) {
        for (Student student : students) {
            System.out.println("Name:" + student.name + ",Id:" + student.id + ",Age:" + student.age);
        }
    }

    public static void printArray(Integer[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public static class AComp implements Comparator<Integer> {

        @Override
        public int compare(Integer o1, Integer o2) {
            return o1 - o2;
        }
    }

    public static void main(String[] args) {
        Integer[] arr = {5, 4, 3, 2, 7, 9, 1, 0};
        Arrays.sort(arr, new AComp());
        printArray(arr);

        Student student1 = new Student("A", 4, 40);
        Student student2 = new Student("B", 4, 21);
        Student student3 = new Student("C", 3, 12);
        Student student4 = new Student("D", 3, 62);
        Student student5 = new Student("E", 3, 42);
        Student[] students = new Student[]{student1, student2, student3, student4, student5};
        System.out.println("第一条打印期望: D E C A B");

        Arrays.sort(students, new IdShengAgeJiangOrder());
        printStudents(students);

        System.out.println("第二条打印：Student排序");
        ArrayList<Student> studentList = new ArrayList<>();
        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);
        studentList.add(student4);
        studentList.add(student5);
        studentList.sort(new IdShengAgeJiangOrder());
        for (int i = 0; i < studentList.size(); i++) {
            Student student = studentList.get(i);
            System.out.println(student.name + "," + student.id + "," + student.age);
        }

        //N*logN
        System.out.println("第三条打印：有序表");
        student1 = new Student("A", 4, 40);
        student2 = new Student("B", 4, 21);
        student3 = new Student("C", 4, 12);
        student4 = new Student("D", 4, 62);
        student5 = new Student("E", 4, 42);

        //有序表
        TreeMap<Student, String> treeMap = new TreeMap<>((a, b) -> (a.id - b.id));
        treeMap.put(student1, "我是学生1，我的名字叫A");
        treeMap.put(student2, "我是学生2，我的名字叫B");
        treeMap.put(student3, "我是学生3，我的名字叫C");
        treeMap.put(student4, "我是学生4，我的名字叫D");
        treeMap.put(student5, "我是学生5，我的名字叫E");
        for (Student student : treeMap.keySet()) {
            System.out.println(student.name + "," + student.id + "," + student.age);
        }
    }
}
